/****************************************************
	文件：Root.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/7/11 16:26:30
	功能：入口
*****************************************************/

using System;
using UnityEngine;

namespace HTSpider
{
    public class Root : MonoBehaviour
    {
        private EngineMgr mEngineMgr;
        private StockSpider mStockSpider; 
        private void Awake()
        {
            InitModule();
        }
        private void Start()
        {
            //string url = "https://www.baidu.com/robots.txt";
            //string txt = DownloadMgr.Instance.Post(url,"", Consts.EncodingType.UTF8);
            //Logger.Log(txt);
            mStockSpider = new StockSpider();
            mStockSpider.Init();
        }
        private void Update()
        {
            mEngineMgr.Tick();
            if (Input.GetKeyDown(KeyCode.T))
            {
                mStockSpider.CrawlingAll();
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                foreach (var item in SpiderMgr.Instance.GetAllRsp())
                {
                    Logger.Log(item.Content);
                }
            }
            if (Input.GetKeyDown(KeyCode.G))
            {
                mStockSpider.ParseRsp();
            }
            if (Input.GetKeyDown(KeyCode.Y))
            {
                double[] data = new double[] { 5, 8, 9, 12, 3.2, 4.7, 5.5, 1.16, 7.68, 23.11 };
                double average = MathHT.Average(data);
                double stdev = MathHT.Standard_Dev(data);
                double normDist = 0.0f;

                TimeSpan timeStart1 = DateTime.Now.TimeOfDay;
                int loop = 1000000;
                for (int i = 0; i < loop; i++)
                {
                    normDist = MathHT.Norm_Dist_Left(8, average, stdev, MathHT.NormDistCalcuType.TaylorExpansion);
                }
                TimeSpan timeEnd1 = DateTime.Now.TimeOfDay;
                Logger.WarningFormat("执行{0}次泰勒展开，耗时：{1}秒，结果：{2}", loop, (timeEnd1 - timeStart1).Seconds, normDist);

                TimeSpan timeStart2 = DateTime.Now.TimeOfDay;
                for (int i = 0; i < loop; i++)
                {
                    //normDist = MathHT.Norm_Dist_Left(8, average, stdev, MathHT.NormDistCalcuType.NormMap);
                }
                TimeSpan timeEnd2 = DateTime.Now.TimeOfDay;
                //Logger.WarningFormat("执行{0}次查表求值，耗时：{1}秒，结果：{2}", loop, (timeEnd2 - timeStart2).Seconds, normDist);
            }
        }
        private void OnDestroy()
        {
            mEngineMgr.OnDestroy();
            mEngineMgr = null;
#if UNITY_EDITOR
            UnityEditor.AssetDatabase.Refresh();
#endif
        }

        private void InitModule()
        {
            mEngineMgr = EngineMgr.Instance;
            mEngineMgr.Init(Debug.Log, Debug.LogWarning, Debug.LogError);
            Logger.Log("All module init done.");
        }
    }
}
